package com.jihu.design.structural.flyweight;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 享元和原型
 * 1.享元返回的是这个本人
 * 2.原型返回的是克隆人
 *
 * @author jihu
 * @date 2021/9/26
 */
public class ZuDao {

    private static Map<String,AbstractWaitressFlyWeight> pool = new HashMap<>();
    //享元:池子中有对象

    static {
        BeautifuWaitress w1 = new BeautifuWaitress("1111","张三",18);

        BeautifuWaitress w2 = new BeautifuWaitress("9527","李四",20);


        pool.put(w1.id,w1);
        pool.put(w2.id,w2);

    }


    public void addWaitress(AbstractWaitressFlyWeight waitressFlyWeight){
        pool.put(UUID.randomUUID().toString(),waitressFlyWeight);
    }

    public static AbstractWaitressFlyWeight getWaitress(String name){
        AbstractWaitressFlyWeight flyWeight = pool.get(name);
        if(flyWeight == null){
            for (AbstractWaitressFlyWeight value : pool.values()) {
                //当前共享对象能否是否
                if(value.isCanService()){
                    return value;
                }
            }
            return null;
        }else {
            return flyWeight;
        }

    }
}
